package newcoder_exp.justPractise;

import java.util.*;

import org.junit.Test;
public class NC52 {
    public class Solution {
        public boolean isValid (String s) {
            if (s == null || s.length() == 0) return true;
            boolean res = true;
            Stack<Character> stack = new Stack<>();
            
            loop: for (char c: s.toCharArray()) {
                if (c == ')' || c == ']' || c == '}') {
                    if (stack.isEmpty()) {
                        res = false;
                        break;
                    }
                    char t = stack.peek();
                    switch(t) {
                        case '(':
                            if (c != ')') {
                                 res = false;
                                 break loop;
                            } else {
                                stack.pop();
                                break;
                            }
                        case '[':
                            if (c != ']') {
                                 res = false;
                                 break loop;
                            } else {
                                stack.pop();
                                break;
                            }
                        case '{':
                            if (c != '}') {
                                 res = false;
                                 break loop;
                            } else {
                                stack.pop();
                                break;
                            }
                    }
                } else {
                    stack.push(c);
                } 
            }
            
            if (!stack.isEmpty())
                res = false;
            return res;
        }
        
    }

    @Test
    public void test() {
        String st = "([)]";
        Solution s = new Solution();
        boolean res = s.isValid(st);
        System.out.println(res);
    }
}